﻿using System.Windows.Input;
using Oxage.Drivers;
using Oxage.Commands;
using Oxage.Common;
using Oxage.Views;

namespace Oxage.Models
{
	public class MainViewModel : ViewModelBase
	{
		#region Constructor
		public MainViewModel()
		{
			this.Document = new DocumentViewModel();

			//Sample G code document
			this.Document.Content =
@";comment
{another comment}
N001 X0 Y0 Z0 G00  ; G90 absolute coordinates by default
N002 X100 Y100 Z0 G01
N003 X0 Y200 Z0 G01
N004 X100 Y300 Z0 G01
N005 X0 Y400 Z0 G01
N006 X100 Y500 Z0 G03";

			//ApplicationCommands.Save
			this.ApplicationCommand = new ApplicationCommand();
			this.DocumentCommand = new DocumentCommand() { Document = Document };
			this.EditCommand = new EditCommand();
			this.DebugCommand = new DebugCommand();
			this.SimulatorCommand = new SimulatorCommand();
			//this.WindowCommand = new WindowCommand();

			//Default controller
			//var port = new SerialPortMachineController();
			//port.SimulatorBlockReceived += (sender, e) =>
			//{
			//  if (View != null)
			//  {
			//    View.Simulator.ViewModel.SimulateBlock(e.Result);
			//  }
			//};
			//this.machineController = port;

#if DEBUG
			//For debugging on machine without serial port
			//this.machineController = new VirtualMachineController();
#endif
		}
		#endregion

		#region Properties
		public MainView View
		{
			get;
			set;
		}

		public DocumentViewModel Document
		{
			get
			{
				return this.document;
			}
			set
			{
				if (this.document != value)
				{
					this.document = value;
					OnPropertyChanged("Document");
				}
			}
		}
		private DocumentViewModel document;

		/// <summary>
		/// Gets or sets a global instance of IMachineController so it can be accessed from other windows.
		/// </summary>
		public IConnector MachineController
		{
			get
			{
				return this.machineController;
			}
			set
			{
				if (this.machineController != value)
				{
					this.machineController = value;
					OnPropertyChanged("MachineController");
				}
			}
		}
		private IConnector machineController;

		public ICommand ApplicationCommand
		{
			get
			{
				return this.applicationCommand;
			}
			set
			{
				if (this.applicationCommand != value)
				{
					this.applicationCommand = value;
					OnPropertyChanged("ApplicationCommand");
				}
			}
		}
		private ICommand applicationCommand;

		public ICommand DocumentCommand
		{
			get
			{
				return this.documentCommand;
			}
			set
			{
				if (this.documentCommand != value)
				{
					this.documentCommand = value;
					OnPropertyChanged("DocumentCommand");
				}
			}
		}
		private ICommand documentCommand;

		public ICommand EditCommand
		{
			get
			{
				return this.editCommand;
			}
			set
			{
				if (this.editCommand != value)
				{
					this.editCommand = value;
					OnPropertyChanged("EditCommand");
				}
			}
		}
		private ICommand editCommand;

		public ICommand DebugCommand
		{
			get
			{
				return this.debugCommand;
			}
			set
			{
				if (this.debugCommand != value)
				{
					this.debugCommand = value;
					OnPropertyChanged("DebugCommand");
				}
			}
		}
		private ICommand debugCommand;

		public ICommand SimulatorCommand
		{
			get
			{
				return this.simulatorCommand;
			}
			set
			{
				if (this.simulatorCommand != value)
				{
					this.simulatorCommand = value;
					OnPropertyChanged("SimulatorCommand");
				}
			}
		}
		private ICommand simulatorCommand;

		public ICommand WindowCommand
		{
			get
			{
				return this.windowCommand;
			}
			set
			{
				if (this.windowCommand != value)
				{
					this.windowCommand = value;
					OnPropertyChanged("WindowCommand");
				}
			}
		}
		private ICommand windowCommand;
		#endregion
	}
}
